package ualrcdg.files;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static XMLHelper xmlhelper = null;

	public static XMLHelper getXMLHelper() {
		return xmlhelper;
	}

	public static void setSitesList(XMLHelper xmlhelper) {
		MyXMLHandler.xmlhelper = xmlhelper;
	}

	// Called when tag starts 
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("maintag"))
		{
			/** Start */ 
			xmlhelper = new XMLHelper();
		} else if (localName.equals("gameid")) {
			/** Get attribute value */ 
			String attr = attributes.getValue("type");
			xmlhelper.setType(attr);
		}

	}

	// Called when tag closing 
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */ 
		if (localName.equalsIgnoreCase("filename"))
			xmlhelper.setFileName(currentValue);
		else if (localName.equalsIgnoreCase("gameid"))
			xmlhelper.setGameID(currentValue);
		else if (localName.equalsIgnoreCase("location"))
			xmlhelper.setLocation(currentValue);		

	}

	// Called to get tag characters
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
